﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Engine
{
    public class MouseEvent : Event
    {

        /// <summary>
        /// Defines the buttons that you can scan for in mouse events
        /// </summary>
        public enum ButtonStyle
        {
            LeftClicked = 4,
            RightClicked = 32,
        }

        /// <summary>
        /// Zone on which the event trigger
        /// </summary>
        public Rectangle triggerZone;

        /// <summary>
        /// Buttons on which the event trigger
        /// </summary>
        public ButtonStyle triggerButtons;
        
        /// <summary>
        /// Should we use Zone triggers
        /// </summary>
        public bool triggerOnZone;

        /// <summary>
        /// Should we use button triggers
        /// </summary>
        public bool triggerOnButtons;

        /// <summary>
        /// Effects to apply when the event succeeds
        /// </summary>
        public List<Effects.Effect> effects = new List<Effects.Effect>();

        /// <summary>
        /// Processes the event and triggers
        /// </summary>
        public override void Update(GameTime gameTime, Input.Mouse mouse)
        {

            //Check for buttons
            bool buttonsValid = false;
            if (this.triggerOnButtons == true)
            {
                if ((this.triggerButtons & ButtonStyle.LeftClicked) == ButtonStyle.LeftClicked && mouse.LeftClickCompleted)
                {
                    buttonsValid = true;
                }
                if ((this.triggerButtons & ButtonStyle.RightClicked) == ButtonStyle.RightClicked && mouse.RightClickCompleted)
                {
                    buttonsValid = true;
                }
            }
            else
            {
                buttonsValid = true;
            }

            //Check for zone
            bool zoneValid = false;
            if (this.triggerOnZone == true)
            {
                if (mouse.CurrentPosition.X >= this.triggerZone.Left
                    && mouse.CurrentPosition.X <= this.triggerZone.Right
                    && mouse.CurrentPosition.Y >= this.triggerZone.Top
                    && mouse.CurrentPosition.Y <= this.triggerZone.Bottom)
                {
                    zoneValid = true;
                }
            }
            else
            {
                zoneValid = true;
            }

            //The event must be fired, apply the effects
            foreach (Effects.Effect e in this.effects)
            {
                e.apply();
            }

        }

    }
}
